using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Windows.Forms;

namespace ThePushCSharp
{
    public class ThePush : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public GraphicsDevice device;

        public Texture2D backgroundTexture;
        public Texture2D foregroundTexture;

        public GameState state;
        public Server server;
        public Client client;
        public int port;

        public int screenWidth;
        public int screenHeight;

        public ThePush()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            updateDimensions(1024, 768, false);
            Window.Title = "The Push";

            port = 6015;
            state = new GameState(this, 10, 2);
            server = new Server(this);
            client = new Client(this);

            base.Initialize();
            Cursor.Show();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;

            backgroundTexture = Content.Load<Texture2D>("background");
            foregroundTexture = Content.Load<Texture2D>("foreground");
        }

        public void updateTitle(String newTitle)
        {
            Window.Title = newTitle;
        }

        public void updateDimensions(int width, int height, bool fullScreen)
        {
            device = graphics.GraphicsDevice;
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            graphics.IsFullScreen = fullScreen;
            graphics.ApplyChanges();

            screenWidth = device.PresentationParameters.BackBufferWidth;
            screenHeight = device.PresentationParameters.BackBufferHeight;
        }

        protected override void UnloadContent()
        {
            client.close();
            server.close();
        }

        protected void keyControls(GameTime gameTime)
        {
            KeyboardState keybState = Keyboard.GetState();
            if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
                state.renderer.camera.moveLeft();
            else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
                state.renderer.camera.moveRight();
            else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
                state.renderer.camera.moveUp();
            else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
                state.renderer.camera.moveDown();
            else if (keybState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
                this.Exit();
        }

        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        protected override void Update(GameTime gameTime)
        {
            keyControls(gameTime);

            base.Update(gameTime);
        }

        /// This is called when the game should draw itself.
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            state.renderer.render();

            spriteBatch.End();

            base.Draw(gameTime);
        }


    }
}
